<template>
   <div>
    <el-dialog  
        v-bind="$attrs" 
        v-on="$listeners"
        :visible="Visible"
        width="40%"
        custom-class="dialog"
        :modal-append-to-body='false'
        append-to-body
        @close="close" 
        :title="formListTitle"
        :destroy-on-close='true'
        :close-on-click-modal='false'
        :close-on-press-escape='false'
        :center='true'
        >
        <el-form ref="inStockManageForm" :model="formListData" :rules="rules" label-width="100px" v-if="viewMark" :inline="true" id="printTest">

            <el-form-item label="出库编号：">
              <span>{{listData.objectId}}</span>
            </el-form-item>
            <el-form-item label="配件名称：">
              <span>{{listData.stock.parts.name}}</span>
            </el-form-item>

             <el-form-item label="出库数量：" >
              <span>{{listData.amount}}</span>
            </el-form-item>

            <el-form-item label="货架编号：" >
             <span>{{listData.stock.stallsNo}}</span>
            </el-form-item>

             <el-form-item label="存储仓库："  >
              <span>{{listData.stock.storage.name}}</span>
            </el-form-item>

             <el-form-item label="生产来源：" >
              <span>{{listData.stock.source.name}}</span>
            </el-form-item>

             <el-form-item label="出库去向：" >
              <span>{{listData.toEnd.name}}</span>
            </el-form-item>

             <el-form-item label="出库人员：" >
              <span>{{listData.operator.name}}</span>
            </el-form-item>

             <el-form-item label="是否合格：">
                  <span>{{listData.stock.status?'合格':'不合格'}}</span>
            </el-form-item>
        </el-form>
         <el-form ref="outStockManageForm" :model="formListData" :rules="rules" label-width="100px" v-else>
            <el-form-item label="配件名称：">
              <span>{{formListData.parts.name}}</span>
            </el-form-item>
             <el-form-item label="当前库存：">
              <span>{{listData.amount}}</span>
            </el-form-item>
            <el-form-item label="配件质量：">
              <span>{{listData.status?'合格':'不合格'}}</span>
            </el-form-item>
            <el-form-item label="出库数量" prop="outAmount">
                <el-input v-model="formListData.outAmount" placeholder="请填写出库数量" clearable :style="{width: '95%'}">
                </el-input>
            </el-form-item>
             <el-form-item label="出库去向" prop="toEnd">
                <el-select v-model="formListData.toEnd" placeholder="请选择" value-key='objectId' :style="{width: '95%'}">
                <el-option
                    v-for="item in toendList"
                    :key="item.value.objectId"
                    :label="item.label"
                    :value="item.value">
                </el-option>
            </el-select>
            </el-form-item>

         </el-form>
         <div slot="footer" v-if="!viewMark">
            <el-button @click="close">取消</el-button>
            <el-button type="primary" @click="handelConfirm" :loading="isloading">确定</el-button>
        </div>
         <div slot="footer" v-else>
            <el-button @click="close">取消</el-button>
            <el-button type="primary" v-print="'#printTest'">打印</el-button>
        </div>
    </el-dialog>
   </div>
</template>

<script>
export default {
  props: {
      Visible:Boolean,
      toendList:Array,
      listData:Object,
      viewMark:Boolean
  },
  components: {

  },
  data() {
      const blurText = async(rule, value, callback) => {  
        const boolean = new RegExp('^[1-9][0-9]*$').test(value)
        if (!boolean) {
          callback(new Error('输入必须为整数'))
        }else if(value===''){
          callback(new Error('请输入出库数量'))
        }else if(Number(value)>this.listData.amount){
          callback(new Error('输入必须小于等于当前库存量'))
        }else{
          callback()
        }
   　}
    return {
        isloading:false,
        inheritAttrs: false,
        formData: {
        },
        rules: 
        {
        outAmount:[
          { required:true,trigger:'change',message:'',validator: blurText}
        ],
        toEnd:[
          { required:true,trigger:'change',message:'请选择'}
        ],
        }
    };
  },
  computed: {
      formListData(){
            this.formData = this.listData
            return this.formData
      },
      formListTitle(){
        if(this.viewMark) return '出库单'
        return  '填写出库单'
      }
  },
  watch: {

  },
  created() {

  },
  mounted() {

  },
  methods: {
    close() {
      this.$emit('close')
    },
    handelConfirm() {
      this.$refs['outStockManageForm'].validate(valid => {
        if (valid &&!this.isloading){
            this.isloading=true
            // if(Number(this.formData.outAmount)<this.listData.parts.lowamount){
            //     this.$utils.message.warning(this,'当前出库数量少于配件入库的最小数量')
            // }else{
                this.saveData()
            //}
            
        }else{
            this.$utils.message.warning(this,'请填写完整信息')
            return false;
        }
        
      })
    },
     saveData(){
        const query = this.$Bmob.Query('OutStock')
        const pointer1 = this.$Bmob.Pointer('Parts')
        const poiID1 = pointer1.set(this.listData.parts.objectId)
        const pointer2 = this.$Bmob.Pointer('Stock')
        const poiID2 = pointer2.set(this.listData.objectId)
        const pointer3 = this.$Bmob.Pointer('Farms')
        const poiID3 = pointer3.set(this.formData.toEnd.objectId)
        const pointer4 = this.$Bmob.Pointer('_User')
        const poiID4 = pointer4.set(this.UserId)
        query.set('parts',poiID1)
        query.set('stock',poiID2)
        query.set('toEnd',poiID3)
        query.set('amount',Number(this.formData.outAmount))
        query.set('operator',poiID4)
        query.save().then(async res=>{
            await this.updateStorageLimitSpace(this.listData.storage.objectId,Number(this.formData.outAmount))
            await this.updateStockAmount(this.listData.objectId,Number(this.formData.outAmount))
            this.$emit('save')
            this.isloading=false
            this.close()
            this.$utils.message.success(this,'保存成功')
        }).catch(err=>{
            this.isloading=false
            this.$utils.message.error(this,'保存失败')
        })
    },
    updateStorageLimitSpace(id,amount){
        const query = this.$Bmob.Query('wareHouse')
        return query.get(id).then(res => {
            res.increment('upperlimit',amount)
            res.save()
        }).catch(err=>{
        })
    },
    updateStockAmount(id,amount){
        const query = this.$Bmob.Query('Stock')
        return query.get(id).then(res => {
            res.increment('amount',-amount)
            res.save()
        }).catch(err=>{
        })
    }
  },
};
</script>

<style scoped lang="scss">

/deep/.el-dialog {
      border-radius: 15px;
      z-index: 5;
      min-width: 620px;  
      margin-top: 5vh !important;
      text-align: center;
}

/deep/ .el-form-item__label {
    font-weight: 600;
    font-size: 15px;
}

span {
    border:1px solid #f1f1f1;
    border-radius: 5px;
    padding: 10px;
}

</style>
